package com.yanqu.road.dao.impl.crosschannel;

import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.servercenter.XServerName;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XServerNameDaoImpl extends TempDataObjectDao<XServerName> {

    private String dbName;
    private String tableName;

    public XServerNameDaoImpl(String dbName, String tableName) {
        this.dbName = dbName;
        this.tableName = tableName;
    }

    public List<XServerName> getXServerNameList() {
        List<XServerName> list = new ArrayList<>();
        String getXServerNameSql = "select * from `" + tableName + "`";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(dbName);
            call = getDb().getConnection().prepareCall(getXServerNameSql);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    @Override
    protected boolean updateData(XServerName xServerName) {
        String updateXServerNameSql = "update `" + tableName + "` set name=? where channelId=? and serverId=?";
        xServerName.setBeginWriteTime();
        getDb().setConnName(dbName);
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, xServerName.getName()));
        para.put(i++, new DbParameter(Types.INTEGER, xServerName.getChannelId()));
        para.put(i++, new DbParameter(Types.BIGINT, xServerName.getServerId()));
        if (getDb().executeNoneQuery(updateXServerNameSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean addData(XServerName xServerName) {
        String insertXServerNameSql = "insert into `" + tableName + "`(channelId,serverId,name) values (?,?,?)";
        xServerName.setBeginWriteTime();
        getDb().setConnName(dbName);
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, xServerName.getChannelId()));
        para.put(i++, new DbParameter(Types.BIGINT, xServerName.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, xServerName.getName()));
        if (getDb().executeNoneQuery(insertXServerNameSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected XServerName parseData(ResultSet rs) throws SQLException {
        int channelId = rs.getInt("channelId");
        long serverId = rs.getLong("serverId");
        String name = rs.getString("name");
        return new XServerName(channelId, serverId, name);
    }
}
